package com.chenggong.demo.activity

import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.UserHandle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type


/**
 *
 */
abstract class BaseActivity<T : ViewDataBinding>(private val useBinding: Boolean) : AppCompatActivity() {

    lateinit var mDataBinding: T

    abstract val layoutId: Int? // xml布局文件，子类指定

    /**
     * UI初始化，[子类必须实现]
     * 会在onCreate方法中获取DataBinding和ViewModel之后被调用
     */
    protected abstract fun initView(savedInstanceState: Bundle?)

    /**
     * 绑定监听事件
     */
    open fun bindEvent() {}

    /**
     * 数据初始化，[子类可选实现]
     * 会在onCreate方法中initView方法之后被调用
     */
    open fun initData(savedInstanceState: Bundle?) {}

    /**
     * 提供ViewModelFactory
     * @return ViewModelProvider.Factory
     */
    open fun provideViewModelFactory(): ViewModelProvider.Factory {
        return ViewModelProvider.AndroidViewModelFactory(application);
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (useBinding) {
            mDataBinding = providerViewDataBinding()
            mDataBinding.lifecycleOwner = this
        } else {
            layoutId?.let { setContentView(it) }
        }

        hideStatusBar()
        initView(savedInstanceState)
        bindEvent()
        initData(savedInstanceState)
    }

    protected fun hideStatusBar() {
        // 5.0以上系统状态栏透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            //白色SYSTEM_UI_FLAG_LAYOUT_STABLE、深色SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.TRANSPARENT
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
    }

    /**
     * 获取DataBinding
     */
    private fun providerViewDataBinding(): T {
        return DataBindingUtil.setContentView(this, layoutId!!)
    }

    /**
     * 设置状态栏颜色
     * @param color String
     */
    protected fun setStatusBarColor(color: String) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = Color.parseColor(color)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        if (newConfig.fontScale != 1.0f) {
            resources
        }
        super.onConfigurationChanged(newConfig)
    }

    override fun getResources(): Resources? {
        val res: Resources? = super.getResources()
        if (res != null) {
            val config: Configuration? = res.configuration
            if (config != null && config.fontScale != 1.0f) {
                config.fontScale = 1.0f
                res.updateConfiguration(config, res.displayMetrics)
            }
        }
        return res
    }
}